﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Net.Mime.MediaTypeNames;

namespace RemoveFunction
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            try
            {
                InitializeComponent(); 
                textBox1.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='DataBase'");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要删除勾选功能？", "疑问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    List<int> list = new List<int>();
                    foreach (TreeNode item in treeView1.Nodes)
                    {
                        for (int i = item.Nodes.Count - 1; i >= 0; i--)
                        {
                            if (item.Nodes[i].Checked)
                            {
                                //list.Add(item.Nodes[i].Text.ToString());
                                list.Add(Convert.ToInt32(item.Nodes[i].Tag.ToString()));
                                item.Nodes.Remove(item.Nodes[i]);
                            }
                        }
                    }
                    //MessageBox.Show(string.Join(",", list));
                    string sql = $@"
                      delete from FunctionList where ID in ({string.Join(",", list)})
                      delete from RoleFunctionInfos where FunctionId in ({string.Join(",", list)})
                    ";
                    SqlRevision(sql);
                }
                MessageBox.Show("删除成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
        public void SqlRevision(string commandText)
        {
            using (SqlConnection conn = new SqlConnection(textBox1.Text))
            {
                SqlCommand cmd = new SqlCommand(commandText, conn);
                if (conn.State != ConnectionState.Open)
                    conn.Open();
                cmd.Connection = conn;
                cmd.CommandText = commandText;
                //int recordsAffected =
                cmd.ExecuteNonQuery();
                //return recordsAffected;
            }

        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                treeView1.Nodes.Clear();
                DataTable dt = SqlGetDT("select Id,FunctionName,FunctionCategory from FunctionList");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    if (treeView1.Nodes.Cast<TreeNode>().FirstOrDefault(node => node.Text == dt.Rows[i]["FunctionCategory"].ToString()) == null)
                    {
                        TreeNode tn = new TreeNode();
                        tn.Text = dt.Rows[i]["FunctionCategory"].ToString();
                        treeView1.Nodes.Add(tn);
                        tn.Nodes.Add(new TreeNode() { Text = dt.Rows[i]["FunctionName"].ToString(), Tag = dt.Rows[i]["Id"].ToString() });
                    }
                    else
                    {
                        treeView1.Nodes.Cast<TreeNode>().FirstOrDefault(node => node.Text == dt.Rows[i]["FunctionCategory"].ToString())
                            .Nodes.Add(new TreeNode() { Text = dt.Rows[i]["FunctionName"].ToString(), Tag = dt.Rows[i]["Id"].ToString() }); ;
                        //treeView1.Nodes[treeView1.Nodes.IndexOfKey(dt.Rows[i]["FunctionCategory"].ToString())]
                    }
                }
                treeView1.ExpandAll();
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }
        public DataTable SqlGetDT(string CommandText)
        {
            using (SqlConnection conn = new SqlConnection(textBox1.Text))
            {
                SqlDataAdapter sda = new SqlDataAdapter(CommandText, conn);
                DataSet ds = new DataSet();
                sda.Fill(ds, "Table");
                return ds.Tables["Table"];
            }
        }


        private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Nodes.Count > 0)
            {
                e.Node.Nodes.Cast<TreeNode>().ToList().ForEach(n => n.Checked = e.Node.Checked);
            }
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                textBox1.Text = SqliteoneQuery("select ConfigValue From ConfigSetting where ConfigKey='DataBase'");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                SqliteoneQuery($"Update ConfigSetting set ConfigValue='{textBox1.Text}' where ConfigKey='DataBase'");
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); }
        }

        public static string SqliteoneQuery(string CommandText)
        {
            using (SQLiteConnection conn = new SQLiteConnection($"Data Source={AppDomain.CurrentDomain.BaseDirectory}ConfigInfo.db;Version=3;Password=XDT20231025"))
            {
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand();
                cmd.Connection = conn;
                cmd.CommandText = CommandText;
                return cmd.ExecuteScalar()?.ToString();
                //if (cmd.ExecuteScalar() != null) return cmd.ExecuteScalar().ToString();
                //else return string.Empty;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory);
        }

    
        public  string SqloneQuery(string commandText)
        {
            using (SqlConnection conn = new SqlConnection(textBox1.Text))
            {
                conn.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = commandText;
                return cmd.ExecuteScalar()?.ToString() ?? string.Empty;
            }
        }
    }

    class MenuControl
    {
     
        public string Code { get; set; }
        public string 菜单名称 { get; set; }
        public string 公司简写名称 { get; set; }
        public string 公司全写名称 { get; set; }
        public string 公司网站 { get; set; }
    }
}
